package com.qf.springboot2203_02.controller;

import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.kaptcha.Kaptcha;
import com.qf.springboot2203_02.dao.UserDao;
import com.qf.springboot2203_02.entity.User;
import com.qf.springboot2203_02.service.UserService;
import com.qf.springboot2203_02.vo.ResultVo;
import com.qf.springboot2203_02.vo.UserResultVo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.swing.plaf.nimbus.NimbusLookAndFeel;
import java.io.IOException;

@SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
@RestController
@RequestMapping("/kaptcha")
@MapperScan(basePackages = "com.qf.springboot2203_02.dao")//优选
public class KaptchaController {
    @Autowired
    Kaptcha kaptcha;
    @Autowired
    UserService userService;

    @GetMapping("/render")
    public void render() {
        kaptcha.render();//生成验证码图片并返回
    }

    @PostMapping("/valid")
    public ResultVo login(@RequestParam String captcha, String username, String password) {
        kaptcha.validate(captcha, 20); //返回值布尔
        ResultVo vo = userService.login(username, password);
        System.out.println(vo);
        return vo;
    }

    @GetMapping("/validTime")
    public void validTime(@RequestParam String code) {
        kaptcha.validate(code, 60);
    }

    @PostMapping("/regist")
    public ResultVo regist(UserResultVo userResultVo) throws IOException {
        kaptcha.validate(userResultVo.getCaptcha(), 60); //返回值布尔
        ResultVo vo = userService.getRegist(userResultVo);
        return vo;
    }
}